package com.bwie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.UserMapper;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.utils.R;
import com.bwie.vo.UserVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    /**
     * @description: 用户账号密码登录
     * @author: xushuaihang
     * @Date: 2024/6/17 9:31
     * @param:
     * @param userVo
     * @return: com.bwie.utils.R
    **/
    @Override
    public R userLogin(UserVo userVo) {

        User user = userMapper.selectOne(new QueryWrapper<User>()
                .lambda().eq(User::getAccount, userVo.getAccount()));

        if(ObjectUtils.isEmpty(user)){
            return R.error().message("用户未注册！");
        }
        if(!user.getPassword().equals(userVo.getPassword())) {
            return R.error().message("密码输入错误！");
        }

        String compact = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie").claim("id", user.getId()).compact();

        return R.ok().message("登录成功！").data("token",compact);
    }
}
